import { queryRecordList, insertRecordList } from '../../api/api.js'
const handleDate = require('../../utils/util.js').handleDate;
const app = getApp();

Page({
  data: {
    params:{
      startDate: "",
      endDate: "",
      id: ""
    },
    minutes: '00',
    seconds: '00',
    startOrEnd: true,
    list:[
      { startTime: '18:31:58', continuedTime: '10s', intervalTime: '2s', status: '注意' },
      { startTime: '18:31:58', continuedTime: '10s', intervalTime: '2s', status: '紧急' },
      { startTime: '18:31:58', continuedTime: '10s', intervalTime: '2s', status: '正常' },
    ],
    tips:[
      '与上次宫缩间隔小于5分钟，可能是临产征兆，要注意哦~',
      '宫缩间隔较长，还需等待，注意记录宫缩哦~'
    ]
  },
  onShow(){
    var self = this;
    wx.getStorage({
      key: 'userId',
      success: function (res) {
        self.setData({ 'params.id': res.data });
        self.queryList();
      },
    })
  },
  onLoad: function () {
    
  },
  startTimer(){
    // 开始时间
    this.setData({ startOrEnd: !this.data.startOrEnd});
    if (!this.data.startOrEnd){
      this.setData({ 'params.startDate': handleDate('yyyy-MM-dd HH:mm:ss', new Date()) })
    }else{
      this.setData({ 'params.endDate': handleDate('yyyy-MM-dd HH:mm:ss', new Date()) })
      var self = this;
      insertRecordList(this.data.params).then(res => {
        self.queryList();
      })
    }
    let second = 0;
    let recordTimer = setInterval(() => {
      if (this.data.startOrEnd){
        // 结束时间
        clearInterval(recordTimer);
        this.setData({ minutes: '00', seconds: '00'})
      }else{
        ++second;
        if (second % 60 < 10) {
          this.setData({ seconds: '0' + second % 60 })
        }
        if (second % 60 >= 10 || second >= 60) {
          if (second % 60 >= 10) {
            this.setData({ seconds: second % 60 });
          }
          let minutes = parseInt(second / 60);
          if (minutes < 10) {
            this.setData({ minutes: '0' + minutes })
          } else {
            this.setData({ minutes: minutes })
          }
        }
      }  
    }, 1000)  
  },
  queryList(){
    queryRecordList({id:this.data.params.id}).then(res=>{
      if(res.data.status == 200){
        this.data.list = res.data.data.map(v=>{
          v.startTime = v.startTime.substr(11,8);
          return v        
        })
        this.setData({ list: this.data.list})
      }
    })
  }
})
